package com.atguigu4.Exercise2;

import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.junit.Test;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Field;

public class ExerciseTest2 {
	
	@Test
	public static <T> List<T> getData(Class<T> clazz,String sql,Object ...args)
	{
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = getConnection();
			ps = conn.prepareStatement(sql);
			for(int i=0;i<args.length;i++)
			{
				ps.setObject(i+1, args[i]);
			}
			rs = ps.executeQuery();
			ResultSetMetaData remd = ps.getMetaData();
			int columnCount = remd.getColumnCount();
			ArrayList<T> list = new ArrayList<>();
			while(rs.next())
			{
				T t = clazz.newInstance();
				for(int i=0;i<columnCount;i++)
				{
					Object columnValue = rs.getObject(i+1);
					String columnLabel = remd.getColumnLabel(i+1);
					java.lang.reflect.Field field = clazz.getDeclaredField(columnLabel);
					field.setAccessible(true);
					field.set(t, columnValue);
				}
				list.add(t);
			}
			 
			return list;
			
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			Close(conn,ps,rs);
		}
		return null;
	}
	
	@Test
	public static void update(String sql,Object ...args)
	{
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = getConnection();
			ps = conn.prepareStatement(sql);
			for(int i=0;i<args.length;i++)
			{
				ps.setObject(i+1, args[i]);
			}
			ps.execute();
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			Close(conn,ps);
		}
	}

	@Test
	public static Connection getConnection() throws Exception
	{
		InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc2.properties");
		Properties pros = new Properties();
		pros.load(is);
		String user = pros.getProperty("user");
		String password = pros.getProperty("password");
		String url = pros.getProperty("url");
		String driverClass = pros.getProperty("driverClass");
		Class.forName(driverClass);
		Connection conn = (Connection) DriverManager.getConnection(url,user,password);
		return conn;
	}
	
	@Test
	public static void Close(Connection conn,PreparedStatement ps)
	{
		if(conn!=null)
		{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(ps!=null)
		{	
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	@Test
	public static void Close(Connection conn,PreparedStatement ps,ResultSet rs)
	{
		if(conn!=null)
		{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(ps!=null)
		{	
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(rs!=null)
		{	
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
